package com.cl.ks.flow.handler;

import com.alibaba.fastjson.JSONObject;
import com.cl.ks.flow.base.BaseFlowNodeHandler;
import com.cl.ks.flow.enums.NodeHandlerEnum;
import com.cl.ks.service.SysSourceConfigService;
import com.cl.ks.service.SysSourceMappingTableFieldService;
import com.cl.ks.service.SysSourceMappingTableService;
import com.cl.ks.utils.DataSourceMap;
import com.cl.ks.utils.DbUtil;
import com.cl.spark.dto.SparkResult;
import com.cl.spark.util.OKHttpUtil;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;


@Component
public class CreateTablePushClueHandler extends BaseFlowNodeHandler {
    private final DbUtil dbUtil;
    private final JPAQueryFactory queryFactory;
    private final CreateTableHandler createTableHandler;
    private final PushClueHandler pushClueHandler;
    @Autowired
    DataSourceMap dataSourceMap;

    @Autowired
    private SysSourceConfigService sysSourceConfigService;
    @Autowired
    private SysSourceMappingTableService sysSourceMappingTableService;
    @Autowired
    private SysSourceMappingTableFieldService sysSourceMappingTableFieldService;

    public CreateTablePushClueHandler(DbUtil dbUtil, JPAQueryFactory queryFactory, CreateTableHandler createTableHandler, PushClueHandler pushClueHandler) {
        this.dbUtil = dbUtil;
        this.queryFactory = queryFactory;
        this.createTableHandler = createTableHandler;
        this.pushClueHandler = pushClueHandler;
    }

    @Override
    public SparkResult process(ProcessParam processParam) {
        SparkResult createTableResult = createTableHandler.process(processParam);
        Map<String, SparkResult> createTableResultMap = new HashMap<>();
        createTableResultMap.put("createTableResult", createTableResult);
        processParam.getSparkNode().setInputSparkResultList(createTableResultMap);

        SparkResult clueResult = pushClueHandler.process(processParam);
        String sceneConfig = processParam.getSparkNode().getNodeExpression().getString("sceneConfig");
        if (StringUtils.hasLength(sceneConfig)) {
            OKHttpUtil.okhttpGetString(sceneConfig, null);
        }

        return clueResult;
    }

    @Override
    public NodeHandlerEnum getType() {
        return NodeHandlerEnum.CREATE_TABLE_PUSH_CLUE;
    }
}
